package igloo.ui;

import igloo.controller.VaultController;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import net.miginfocom.swing.MigLayout;

public class UploadArchiveDialog extends JDialog
{
    private JLabel mDescriptionLabel;
    private JTextPane mDescriptionTextPane;

    private JLabel mFileLabel;
    private JTextField mFileTextField;
    private JButton mBrowseButton;

    private JButton mOKButton;
    private JButton mCancelButton;

    private VaultController mVaultController;

    public UploadArchiveDialog(Frame parent, boolean modal, VaultController controller)
    {
        super(parent, modal);
        
        mVaultController = controller;

        JPanel panel = new JPanel();
        panel.setLayout(new MigLayout("fill", "[grow][]", "[][][][grow][]"));

        panel.add(getFileLabel(), "wrap");
        panel.add(getFileTextField(), "growx, span 2, split 2");
        panel.add(getBrowseButton(), "wrap");
        panel.add(getDescriptionLabel(), "wrap");
        panel.add(new JScrollPane(getDescriptionTextPane()), "grow, span 2");
        panel.add(new JSeparator(), "growx, span 2, wrap");
        panel.add(getOKButton(), "align right, span 2, split 2, tag ok");
        panel.add(getCancelButton(), "tag cancel");

        setContentPane(panel);
    }

    public JLabel getDescriptionLabel()
    {
        if(mDescriptionLabel == null)
        {
            mDescriptionLabel = new JLabel("Description:");
        }

        return mDescriptionLabel;
    }

    public JTextPane getDescriptionTextPane()
    {
        if(mDescriptionTextPane == null)
        {
            mDescriptionTextPane = new JTextPane();
        }

        return mDescriptionTextPane;
    }

    public JLabel getFileLabel()
    {
        if(mFileLabel == null)
        {
            mFileLabel = new JLabel("File:");
        }

        return mFileLabel;
    }

    public JTextField getFileTextField()
    {
        if(mFileTextField == null)
        {
            mFileTextField = new JTextField();
        }

        return mFileTextField;
    }

    public JButton getBrowseButton()
    {
        if(mBrowseButton == null)
        {
            mBrowseButton = new JButton("Browse...");

            mBrowseButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    JFileChooser chooser = new JFileChooser();
                    int option = chooser.showOpenDialog(UploadArchiveDialog.this);
                    if(option == JFileChooser.APPROVE_OPTION)
                    {
                        File file = chooser.getSelectedFile();
                        getFileTextField().setText(file.getAbsolutePath());
                    }
                }
            });
        }

        return mBrowseButton;
    }

    private JButton getOKButton()
    {
        if(mOKButton == null)
        {
            mOKButton = new JButton("OK");
            mOKButton.setEnabled(true);

            mOKButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    String path = getFileTextField().getText();
                    File file = new File(path);
                    mVaultController.createArchive(file, getDescriptionTextPane().getText());
                    dispose();
                }
            });
        }

        return mOKButton;
    }

    private JButton getCancelButton()
    {
        if(mCancelButton == null)
        {
            mCancelButton = new JButton("Cancel");

            mCancelButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    dispose();
                }
            });
        }

        return mCancelButton;
    }

    public static void main(String[] args)
    {
        UploadArchiveDialog dlg = new UploadArchiveDialog(null, true, null);
        dlg.setVisible(true);
    }
}
